var fs = require('fs');
var path = require("path");
var webpack = require('webpack');
var webpackDevMiddleware = require('webpack-dev-middleware');
var webpackHotMiddleware = require('webpack-hot-middleware');
var proxy = require('http-proxy-middleware');
var config = require('./webpack.config');
var qs = require('querystring');
var bodyParser = require('body-parser');
var express = require('express');
//创建一个express应用程序 创建web应用
var app = new (require('express'))();
//设置启动端口号
var port = 8080;
var compiler = webpack(config);
//导入log4js
var log4js = require('log4js');
//导入配置文件
log4js.configure(__dirname + '/logs' + "/log4js.json");
//设置使用哪个日志记录器
var logger = log4js.getLogger('logInfo');
//perec/api/operpos是渠道运营位模块的context,其他模块以后继续加
var apiPerec = proxy('/perec/api/operpos/**', {
    target: 'http://192.168.100.210:8081', changeOrigin: true, onProxyReq(proxyReq, req, res) {
        proxyReq.setHeader('Content-Type', 'application/json');
    }, onError(err, req, res) {
        logger.error("错误的请求地址" + req.address + "错误信息：" + err);
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('代理出错！！！');
    },
});
var apiScene = proxy('/perec/api/scene/**', {
    target: 'http://192.168.100.210:8083', changeOrigin: true, onError(err, req, res) {
        logger.error("错误的请求地址" + req.address + "错误信息：" + err);
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('代理出错！！！');
    },
});
var apiStrategy = proxy('/perec/api/strategy/**', {
    target: 'http://192.168.100.210:8082', changeOrigin: true, onProxyReq(proxyReq, req, res) {
        proxyReq.setHeader('Content-Type', 'application/json');
    }, onError(err, req, res) {
        logger.error("错误的请求地址" + req.address + "错误信息：" + err);
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('代理出错！！！');
    },
});
var apiActivity = proxy('/perec/api/activity/activities/**', {
    target: 'http://192.168.100.210:8084', changeOrigin: true, onProxyReq(proxyReq, req, res) {
        proxyReq.setHeader('Content-Type', 'application/json');
    }, onError(err, req, res) {
        logger.error("错误的请求地址" + req.address + "错误信息：" + err);
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('代理出错！！！');
    },
});
var recquery = proxy('/perec/api/recommend/**', {
    target: 'http://192.168.100.210:8086', changeOrigin: true, onProxyReq(proxyReq, req, res) {
        proxyReq.setHeader('Content-Type', 'application/json');
    }, onError(err, req, res) {
        logger.error("错误的请求地址" + req.address + "错误信息：" + err);
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('代理出错！！！');
    },
});
/*效果评估（内容覆盖率、用户分布、转化率）*/
var  apiEvaluateRate = proxy('/perec/api/evaluate/evaluates/**', {
    target: 'http://192.168.100.210:8089/', changeOrigin: true, onProxyReq(proxyReq, req, res) {
        proxyReq.setHeader('Content-Type', 'application/json');
    }, onError(err, req, res) {
        logger.error("错误的请求地址" + req.address + "错误信息：" + err);
        res.writeHead(500, {
            'Content-Type': 'text/plain'
        });
        res.end('代理出错！！！');
    },
});
app.use(log4js.connectLogger(logger, {level: log4js.levels.INFO}));
app.use(express.static(path.join(__dirname, 'bin')));
app.use(apiPerec, apiScene, apiStrategy, apiActivity,recquery,apiEvaluateRate);
app.use(bodyParser.json({type: ['application/json']}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(webpackDevMiddleware(compiler, {noInfo: true, publicPath: config.output.publicPath}));
if(process.env.npm_lifecycle_event==undefined){
    console.log("热部署启动！");
    app.use(webpackHotMiddleware(compiler));
}
//路由设置 所有/ url访问的都访问index.html
app.get("/*", function (req, res) {
    res.sendFile(__dirname + '/index.html');
})

//启动监听，并且配置监听端口号
app.listen(port, function (error) {
    if (error) {
        console.error(error);
        logger.error("监听失败！");
    } else {
        logger.info("==>   Listening on port %s. Open up http://localhost:%s/ in your browser.", port, port);
    }
})




